package com.org.gen.service.impl;

import com.org.gen.entity.GenTemplateGroup;
import com.org.gen.mapper.GenTemplateConfigMapper;
import com.org.gen.mapper.GenTemplateGroupMapper;
import com.org.gen.service.IGenTemplateGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GenTemplateGroupServiceImpl implements IGenTemplateGroupService {

    @Autowired
    private GenTemplateGroupMapper genTemplateGroupMapper;

    @Autowired
    private GenTemplateConfigMapper genTemplateConfigMapper;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    public List<GenTemplateGroup> listAll() {
        return genTemplateGroupMapper.listAll();
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    public GenTemplateGroup getById(Integer id) {
        return genTemplateGroupMapper.getById(id);
    }

    /**
     * 新增，插入所有字段
     *
     * @param GenTemplateGroup 新增的记录
     * @return 返回影响行数
     */
    public int insert(GenTemplateGroup GenTemplateGroup) {
        return genTemplateGroupMapper.insert(GenTemplateGroup);
    }

    /**
     * 新增，忽略null字段
     *
     * @param GenTemplateGroup 新增的记录
     * @return 返回影响行数
     */
    public int insertIgnoreNull(GenTemplateGroup GenTemplateGroup) {
        GenTemplateGroup.setIsDeleted(0);
        return genTemplateGroupMapper.insertIgnoreNull(GenTemplateGroup);
    }

    /**
     * 修改，修改所有字段
     *
     * @param GenTemplateGroup 修改的记录
     * @return 返回影响行数
     */
    public int update(GenTemplateGroup GenTemplateGroup) {
        return genTemplateGroupMapper.update(GenTemplateGroup);
    }

    /**
     * 修改，忽略null字段
     *
     * @param GenTemplateGroup 修改的记录
     * @return 返回影响行数
     */
    public int updateIgnoreNull(GenTemplateGroup GenTemplateGroup) {
        return genTemplateGroupMapper.updateIgnoreNull(GenTemplateGroup);
    }

    /**
     * 删除记录
     *
     * @param GenTemplateGroup 待删除的记录
     * @return 返回影响行数
     */
    public int delete(GenTemplateGroup GenTemplateGroup) {
        return genTemplateGroupMapper.delete(GenTemplateGroup);
    }

    public int deleteGroup(GenTemplateGroup GenTemplateGroup) {
        List<GenTemplateGroup> GenTemplateGroups = this.listAll();
        if (GenTemplateGroups.size() == 1) {
            throw new RuntimeException("无法删除，必须要有一个模板组");
        }
        int delete = genTemplateGroupMapper.delete(GenTemplateGroup);
        genTemplateConfigMapper.deleteByGroupId(GenTemplateGroup.getId());
        return delete;
    }

    public GenTemplateGroup getByName(String name) {
        return genTemplateGroupMapper.getByName(name);
    }
}